package org.example.response;

import org.apache.commons.lang3.StringUtils;
import org.example.constants.ResCode;

import java.io.Serializable;

/**
 * 返回值
 * @author Kong
 * @date 2021-9-13 13:59
 */
public class R<T> implements Serializable {
    private Integer code;
    private String message;
    private T data;
    private R(){}
    private R(Builder<T> builder) {
        this.code = builder.code;
        this.message = builder.message;
        this.data = builder.data;
    }

    public Integer getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public T getData() {
        return data;
    }

    public static class Builder<T>{
        private Integer code;
        private String message;
        private T data;
        public static Builder getInstance() {
            return new Builder();
        }
        public Builder<T> buildOk() {
            this.code = ResCode.SUCCESS.getCode();
            this.message = ResCode.SUCCESS.getMessage();
            return this;
        }
        public Builder<T> buildFailure(String message) {
            this.code = ResCode.FAILURE.getCode();
            this.message = StringUtils.isNotEmpty(message)? message: ResCode.FAILURE.getMessage();
            return this;
        }
        public Builder<T> buildSystemError(String message) {
            this.code = ResCode.SYSTEM_ERROR.getCode();
            this.message = StringUtils.isNotEmpty(message)? message: ResCode.SYSTEM_ERROR.getMessage();
            return this;
        }
        public Builder<T> buildData(T data) {
            this.data = data;
            return this;
        }
        public Builder<T> buildCustomize(Integer code,String message) {
            this.code = code;
            this.message = message;
            return this;
        }
        public R<T> build() {
            return new R<>(this);
        }
    }
}
